package com.wwwarehouse.contract.release_supply_requirements;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.widget.GridView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.contract.adapter.RsourseChildAdapter;
import com.wwwarehouse.contract.bean.SelectResourseBean;
import com.wwwarehouse.contract.common.ContractCommon;
import com.wwwarehouse.contract.core.ContractConstant;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * @创建者 chenchao.
 * @创建时间 2017/9/22 19:30
 * @描述 ${TODO}
 */

public class SelectResourceFragmentChild extends BaseFragment {

    private View                              mView;
    private GridView                          mGvResourse;
    private StateLayout                       mStateLayout;
    private BigDecimal                        mBigUkid;
    private Map                               mUnPublishMapChild;
    private SelectResourseBean                mSelectResourseBean;
    private List<SelectResourseBean.ListBean> mUnPublishGoodList;
    private RsourseChildAdapter               mRsourseChildAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_resourse_child, container, false);
        return mView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void onEventMainThread(String string) {
        if ("LOCAL_BACK_SELECT_ADAPTER".equals(string)) {
            List<SelectResourseBean.ListBean> selectToCanSeePublishList = ContractCommon.getInstance().getSelectToCanSeePublishList();

            for (int i = 0; i < mUnPublishGoodList.size(); i++) {
                mUnPublishGoodList.get(i).setSelected(false);
                for (int j = 0; j < selectToCanSeePublishList.size(); j++) {
                    if (mUnPublishGoodList.get(i).getDefinedUkid() == selectToCanSeePublishList.get(j).getDefinedUkid()) {
                        mUnPublishGoodList.get(i).setSelected(true);
                        EventBus.getDefault().post("SELECTNUMADD");
                    }

                }
            }
            mRsourseChildAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }

    private void initData() {
        Bundle bundleAsk = getArguments();
        if (bundleAsk != null) {
            String ownerUkid = bundleAsk.getString("ownerUkid");
            int page = bundleAsk.getInt("page");
            mBigUkid = new BigDecimal(ownerUkid);
            mUnPublishMapChild = new HashMap();

            mUnPublishMapChild.put("ownerUkid", mBigUkid);
            mUnPublishMapChild.put("page", page);
            mUnPublishMapChild.put("release", 0);
            mUnPublishMapChild.put("size", 9);
        }
        //请求接口
        getUnPublishListChild();

    }

    private void getUnPublishListChild() {
        mStateLayout.showProgressView(true);//加载时候的状态
        NoHttpUtils.httpPost(ContractConstant.EUERY_UNPUBLISH_LIST, mUnPublishMapChild, mOnResponseListener, 0);
    }

    private void flash() {
        if (ContractCommon.getInstance().isSeleteToHistory()) {
            for (int i = 0; i < mUnPublishGoodList.size(); i++) {
                for (int j = 0; j < ContractCommon.getInstance().getSelectToCanSeePublishList().size(); j++) {
                    LogUtils.showLog("开始对比");
                    if ((mUnPublishGoodList.get(i).getDefinedUkid()) == ContractCommon.getInstance().getSelectToCanSeePublishList().get(j).getDefinedUkid()) {
                        LogUtils.showLog("进来了");
                        mUnPublishGoodList.get(i).setSelected(true);
                        EventBus.getDefault().post("SELECTNUMADD");
                    }
                }
            }
            mRsourseChildAdapter.notifyDataSetChanged();


        }
    }

    private NoHttpUtils.OnResponseListener mOnResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int i) {
            switch (i) {
                case 0:
                    if ("0".equals(commonClass.getCode())) {
                        mSelectResourseBean = JSON.parseObject(commonClass.getData().toString(), SelectResourseBean.class);
                        mUnPublishGoodList = mSelectResourseBean.getList();
                        mGvResourse.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                            @Override
                            public void onGlobalLayout() {
                                mGvResourse.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                                int itemHeight = (mGvResourse.getMeasuredHeight() - mGvResourse.getPaddingTop() - mGvResourse.getPaddingBottom() - mGvResourse.getVerticalSpacing() * (mGvResourse.getNumColumns() - 1) - 28) / mGvResourse.getNumColumns();
                                mRsourseChildAdapter = new RsourseChildAdapter(mActivity, mUnPublishGoodList, itemHeight);
                                mGvResourse.setAdapter(mRsourseChildAdapter);
                                flash();


                            }
                        });


                    } else {
                        mStateLayout.showSystemView(commonClass.getMsg(), true);
                        mStateLayout.setSystemListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                getUnPublishListChild();
                            }
                        });
                    }
                    break;
            }
        }

        @Override
        public void onFailed(String s, int i) {
            mStateLayout.showNetworkView(true);
            mStateLayout.setNetworkListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getUnPublishListChild();
                }
            });
        }

        @Override
        public void onFinish(int i) {
            mStateLayout.showContentView();
        }
    };

    private void initView() {
        mStateLayout = findView(mView, R.id.sl_state);
        mGvResourse = findView(mView, R.id.gv_resourse);

    }

    @Override
    public void setTitle() {

    }

    @Override
    public void requestDatas() {

    }
}
